package com.test.lol;


import android.app.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import com.google.gson.*;
import java.util.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.entity.*;
import org.apache.http.impl.client.*;


public class CommentActivity extends Activity{
	
	private CommentObject commentObj[];
	private DBResponseObject dbResponse;
	private String ID;
	private ListView lstTest;
	private EditText nameField;
	private EditText commentField;  
	private Button sendButton;
	private Button cancelButton;
		
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
        	setContentView(R.layout.commentlistview);    	
        	
        	lstTest = (ListView) findViewById(R.id.lstV);
       
        	Bundle extras = getIntent().getExtras();
        	if(extras == null){
        		return;
        	}
        	ID = extras.getString("ID");
   
        	nameField = (EditText) findViewById(R.id.NameCaptionInput);
        	commentField = (EditText) findViewById(R.id.otherCaptionInput); 
        	sendButton = (Button) findViewById(R.id.buttonSend);
        	cancelButton = (Button) findViewById(R.id.buttonCancel);
        	sendButton.setVisibility(View.GONE);
        	cancelButton.setVisibility(View.GONE);
        	nameField.setVisibility(View.GONE);
        	commentField.setVisibility(View.GONE);
        	
        	getComments(ID);
  
    }
    
    private void getComments(String ID){
    	
    	Gson g = new Gson();
    	String response = "";
		try {
			
        	HttpClient client = new DefaultHttpClient();
        	HttpGet get = new HttpGet(Common.baseUrl+ID+"/comments/");
        	BasicResponseHandler br = new BasicResponseHandler();
			response = client.execute(get, br);

		    commentObj = g.fromJson(response, CommentObject[].class);				    
			ArrayList<CommentObject> commentList = new ArrayList<CommentObject>();			
			
			for(int i = 0; i < commentObj.length; i++){
				commentList.add(commentObj[i]);
			}
	
			lstTest.setAdapter(new CommentArrayAdapter(this, R.layout.list_item, commentList));	    		    	    
		    
		} catch (Exception e) {
			dbResponse = g.fromJson(response, DBResponseObject.class);
			Toast.makeText(CommentActivity.this, dbResponse.getMessage(), Toast.LENGTH_LONG).show();
			Log.e(this.getClass().getName(), dbResponse.getMessage());
		}	
    }

    public void onSend(View view){
    	try{
    		Log.v(this.getClass().getName(), "CommentActivity Photo ID: " + ID);

	    	String sendStr = "{'name': '"+nameField.getText()+"', 'comment': '" +commentField.getText()+"'}";
	    	StringEntity entity = new StringEntity(sendStr);
	    	
	    	
	    	Log.v(this.getClass().getName(), "CommentActivity Send String: " +sendStr);
	    	
	    	BasicResponseHandler br = new BasicResponseHandler();
	    	HttpClient client;
	    	HttpPost post;
	    	String response;	     
	    	
	    	post = new HttpPost(Common.baseUrl+ID+"/comments/");    	   	
	        client = new DefaultHttpClient();
	        entity.setContentType("application/json");
	        post.setEntity(entity);      
        	response = client.execute(post, br); 
        	
        	
        	Gson g = new Gson();      	
        	dbResponse = g.fromJson(response, DBResponseObject.class);

        	Log.v(this.getClass().getName(), "Response: " +dbResponse.getMessage());
		    
        	sendButton.setVisibility(View.GONE);
        	cancelButton.setVisibility(View.GONE);
        	nameField.setVisibility(View.GONE);
        	commentField.setVisibility(View.GONE);
        	
        	this.getComments(ID);
		    
        }catch (Exception e){

        	Log.e(this.getClass().getName(), "Post exception: "+ e.getMessage());
        }
    }    
    
    public void onCancel(View view){
    	sendButton.setVisibility(View.GONE);
    	cancelButton.setVisibility(View.GONE);
    	nameField.setVisibility(View.GONE);
    	commentField.setVisibility(View.GONE);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("Comment");
		menu.add("Refresh");
		return super.onCreateOptionsMenu(menu);
    }

    /*
     * (non-Javadoc)
     * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getTitle() == "Comment"){
        	
        	Log.v(this.getClass().getName(), "if " + item.getItemId()+"");
        	nameField.setVisibility(View.VISIBLE);
        	commentField.setVisibility(View.VISIBLE);
        	sendButton.setVisibility(View.VISIBLE);
        	cancelButton.setVisibility(View.VISIBLE);
        }

        else{
        	this.getComments(ID);         	
        }
        return true;
    }
}
